from tw.api import WidgetsList,CSSLink
from tw.forms import TableForm, CalendarDatePicker, SingleSelectField, TextField, TextArea, Spacer,CheckBox,FileField,HiddenField
from tg import url
from comboselect import ComboSelect
from tw.forms.validators import Int, NotEmpty,UnicodeString,All,ForEach,DateTimeConverter
from datetime import date,datetime
import time
from tw.jquery.autocomplete import AutoCompleteField
from tw.dynforms import HidingTableForm,HidingTableFieldSet,HidingCheckBox
from formencode import Invalid
from datetime import date,datetime
import time

class NakedDateConverter(DateTimeConverter):
    
    def _to_python(self,value,state):
        """
        standardise to 00:00 on the date provide. From this step on, this will be
        accepted as being UTC.
        """
        print value,state
        if value:
            if len(value) > 10:
                value = value[:9]
            if len(value) < 10:
                raise Invalid(self.message('badFormat', state), value, state)
        if value and isinstance(value, (date,datetime)):
            value.hour = 0
            value.minute = 0
            value.second = 0
            return value
        else:
            try:
                tpl = time.strptime(value,self.format)
            except ValueError:
                raise Invalid(self.message('badFormat', state), value, state)
            except Exception,e:
                print e
            # shoudn't use time.mktime() because it can give OverflowError,
            # depending on the date (e.g. pre 1970) and underlying C library
            return datetime(year=tpl.tm_year, month=tpl.tm_mon, day=tpl.tm_mday,
                            hour=0, minute=0,
                            second=0)

